package com.lianzhihui.romeex.ui.orepool;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lianzhihui.romeex.MyApplication;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.RollingPoolAwardListBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.MiningEarningsPresnterImp;
import com.lianzhihui.romeex.mvp.view.MiningEarningsView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.DateUtil;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.widget.ZFEmptyRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.orepool
 * @ClassName: MiningEarningsActivity
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/13 15:10
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/13 15:10
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class MiningEarningsActivity extends BaseActivity implements OnRefreshLoadMoreListener, MiningEarningsView {
    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.state_bar)
    View stateBar;
    @BindView(R.id.message1)
    TextView message1;
    @BindView(R.id.message2)
    TextView message2;
    @BindView(R.id.message3)
    TextView message3;
    @BindView(R.id.message4)
    TextView message4;
    @BindView(R.id.mySmartRefreshLayout)
    SmartRefreshLayout mySmartRefreshLayout;
    @BindView(R.id.myRecyclerView)
    ZFEmptyRecyclerView myRecyclerView;
    private MiningEarningsPresnterImp miningEarningsPresnterImp;
    private int pageSize = 20;
    private MiningEarningsAdapter miningEarningsAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mining_earnings_layout;
    }

    @Override
    protected void initView() {
        includeTopIvBack.setImageResource(R.drawable.icon_return_w);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, stateBar.getHeight());
        layoutParams.topMargin = StatusBarUtil.getStatusBarHeight(MyApplication.mContext);
        stateBar.setLayoutParams(layoutParams);
        includeTopLinBackground.setBackgroundColor(getResources().getColor(R.color.transparent));
        StatusBarUtil.setTransparentForImageView(this, null);
        includeTopTvTitle.setText("我的挖矿收益");
        includeTopTvTitle.setTextColor(getResources().getColor(R.color.white));
        mySmartRefreshLayout.setOnRefreshLoadMoreListener(this);
        myRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        miningEarningsAdapter = new MiningEarningsAdapter(this, new ArrayList<>());
        myRecyclerView.setAdapter(miningEarningsAdapter);
        miningEarningsPresnterImp = new MiningEarningsPresnterImp(this, this);
        mySmartRefreshLayout.autoRefresh();
    }

    @Override
    public boolean setFitSystemWindowFlag() {
        return false;
    }

    @Override
    protected void initData() {
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String[] stringArray = extras.getStringArray(AppConstants.INTENT_MINGS);
            if (null != stringArray) {
                if (stringArray[0] != null && !stringArray[0].isEmpty()) {

                    message1.setText(MessageFormat.format("{0}R", stringArray[0]));
                }
                if (stringArray[1] != null && !stringArray[1].isEmpty()) {

                    message2.setText(MessageFormat.format("{0}R", stringArray[1]));
                }
                if (stringArray[2] != null && !stringArray[2].isEmpty()) {

                    message3.setText(MessageFormat.format("{0}" + getString(R.string.text_rex), stringArray[2]));
                }
                if (stringArray[3] != null && !stringArray[3].isEmpty()) {

                    message4.setText(MessageFormat.format("{0}" + getString(R.string.text_rex), stringArray[3]));
                }


            }
        }
    }

    @OnClick({R.id.include_top_iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_top_iv_back:
                finish();
                break;
            default:
                break;
        }

    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        miningEarningsPresnterImp.requestRollingPoolAwardList(pageNum, pageSize);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        pageNum = 1;
        miningEarningsPresnterImp.requestRollingPoolAwardList(pageNum, pageSize);
    }

    @Override
    public void responseRollingPoolAwardListSuccess(RollingPoolAwardListBean b) {
        if (null != b) {
            if (pageNum == 1) {
                myRecyclerView.showEmptyView(b.getContent().size(), 1);
                miningEarningsAdapter.setNewData(b.getContent());
            } else {
                miningEarningsAdapter.addData(b.getContent());
            }
        }
        pageNum++;
        mySmartRefreshLayout.finishLoadMore();
        mySmartRefreshLayout.finishRefresh();
    }

    @Override
    public void setFailure() {
        mySmartRefreshLayout.finishLoadMore();
        mySmartRefreshLayout.finishRefresh();
    }

    public static class MiningEarningsAdapter extends BaseQuickAdapter<RollingPoolAwardListBean.ContentBean, BaseViewHolder> {
        private Context context;

        public MiningEarningsAdapter(Context context, @Nullable List<RollingPoolAwardListBean.ContentBean> data) {
            super(R.layout.item_mining_earnings_layout, data);
            this.context = context;
        }

        @Override
        protected void convert(@NonNull BaseViewHolder helper, RollingPoolAwardListBean.ContentBean item) {
            helper.setText(R.id.reward, item.getPledgeBalance() + context.getString(R.string.text_rex));

            //发放状态 1已发放 0未发放
            if (item.getIsGrant() == 0) {
                helper.setText(R.id.isGrant, context.getString(R.string.text_not_issued));
                helper.setTextColor(R.id.isGrant, context.getResources().getColor(R.color.text_24254B_color));
                helper.setText(R.id.time, DateUtil.sec2Date(item.getTimeStamp()));
            } else {
                helper.setText(R.id.isGrant, context.getString(R.string.text_issued));
                helper.setTextColor(R.id.isGrant, context.getResources().getColor(R.color.text_F3863B_color));
                helper.setText(R.id.time, DateUtil.sec2Date(item.getAddTime()));
            }
        }
    }
}
